package cn.hchaojie.snippets.list;

import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.hchaojie.snippets.AssetHelper;
import cn.hchaojie.snippets.R;

public class TestList extends ListActivity {
	public static final String TAG = "Test List Activity";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	 super.onCreate(savedInstanceState);
    	 
    	 JSONArray activities = null;
    	 try {
    		 JSONObject root = AssetHelper.readJson(this, "data/all_activities.json");

    		 activities = root.getJSONArray("activities");
    	 } catch (IOException e) {
    		 Log.e(TAG, "Get asset data/all_activities.json error. " + e.getMessage());
    		 // to pop up a dialog
    	 } catch (JSONException e) {
    		 Log.e(TAG, "Parse json data error: " + e.getMessage());
    		 // to pop up a dialog    		 
    	 }
    	 
    	 if (activities != null) {
    		 setListAdapter(new ActivityAdapter(this, activities));
    	 }
    }
    
    private static class ActivityAdapter extends BaseAdapter {
    	private LayoutInflater mInflater;
    	private JSONArray mActivities;
    	
    	public ActivityAdapter(Context context, JSONArray activities) {
    		 mInflater = LayoutInflater.from(context);
    		 mActivities = activities;
    	}
    	
		public int getCount() {
			return mActivities.length();
		}

		public Object getItem(int position) {
			return mActivities.opt(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.activity_list_item, null);

                holder = new ViewHolder();
                holder.imageContactPic = (ImageView) convertView.findViewById(R.id.image_contact_pic);
                holder.txtContactName = (TextView) convertView.findViewById(R.id.txt_contact_name);
                holder.txtContent = (TextView) convertView.findViewById(R.id.txt_activity_content);
                holder.txtTime = (TextView) convertView.findViewById(R.id.txt_activity_time);
                holder.imageActivityType = (ImageView) convertView.findViewById(R.id.icon_activity_type);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            // Bind the data efficiently with the holder.
            JSONObject activity = mActivities.optJSONObject(position);
            if (activity != null) {
            	String type = activity.optString("type");
            	String name = activity.optString("who");
            	String content = activity.optString("content");
            	String time = activity.optString("time");
            	String pic = activity.optString("pic_url");
            	
            	//holder.imageContactPic.setImageBitmap(bm);
            	holder.txtContactName.setText(name);
            	holder.txtContent.setText(content);
            	holder.txtTime.setText(time);
            	holder.imageActivityType.setImageResource(getIconDrawableByType(type));
            }

            return convertView;
		}
		
        static class ViewHolder {
        	ImageView imageContactPic;
            TextView txtContactName;
            TextView txtContent;
            TextView txtTime;
            ImageView imageActivityType;
        }
        
        private int getIconDrawableByType(String type) {
        	if ("call".equalsIgnoreCase(type)) {
        		return R.drawable.icon_call;
        	} else if ("chat".equalsIgnoreCase("type")) {
        		return R.drawable.icon_chat;
        	} else if ("facebook".equalsIgnoreCase("type")) {
        		return R.drawable.icon_facebook;
        	} else if ("twitter".equalsIgnoreCase("type")) {
        		return R.drawable.icon_twitter;
        	}

        	return -1;
        }
    }
}
